package server;

import java.sql.*;
import java.util.ArrayList;

public class Database {

	private Connection conn;
	private Statement stmt;

	public Database() {
		conn = null;
		stmt = null;
	}

	public boolean openConnection() {
		String userName = "db06";
		String password = "adam123";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://puccini.cs.lth.se/" + userName, userName,
					password);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public void closeConnection() {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (SQLException e) {
		}
		conn = null;
	}

	/**
	 * public boolean isConnected(String name) { ResultSet rs = null;
	 * PreparedStatement stmt = null; try { stmt =
	 * conn.prepareStatement("select name from user where name = ?");
	 * stmt.setString(1, name); rs = stmt.executeQuery(); while (rs.next()) {
	 * return true;
	 * 
	 * } } catch (SQLException e) {
	 * 
	 * e.printStackTrace(); }
	 * 
	 * return false; }
	 */

	// public String getHashedPassword(String title, String name) {
	// ResultSet rs = null;
	// PreparedStatement stmt = null;
	// try {
	// stmt = conn.prepareStatement("select hashedpassword from " + title
	// + "s where name = " + name);
	// rs = stmt.executeQuery();
	// while (rs.next()) {
	// return rs.getString("hashedPassword");
	// }
	// } catch (SQLException e) {
	//
	// e.printStackTrace();
	// }
	// return "no existing person";
	// }

	public boolean createPatient(String name) {
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement("insert into patient values ('" + name
					+ "')");
			stmt.executeQuery();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean createEmployee(String title, String name, String division) {
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement("insert into '" + title + "s' values ('"
					+ name + "', '" + division + "')");
			stmt.executeQuery();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean createDivision(String name) {
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement("insert into division values ('" + name
					+ "')");
			stmt.executeQuery();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public ArrayList<String> getPersonRecords(String name, String title,
			String division) {
		ResultSet rs = null;
		PreparedStatement stmt = null;
		ArrayList<String> returnValue = new ArrayList<String>();
		try {
			if (name.compareTo("aggeagency") == 0) {
				stmt = conn.prepareStatement("select * from records");

			} else if (title.compareTo("patient") == 0) {
				stmt = conn
						.prepareStatement("select * from records where patient  = "
								+ "'" + name + "'");
			} else {
				stmt = conn
						.prepareStatement("select * from records where division = "
								+ "'"
								+ division
								+ "'"
								+ "or doctor  = "
								+ "'"
								+ name
								+ "'"
								+ "or nurse  = "
								+ "'"
								+ name
								+ "'");
			}

			rs = stmt.executeQuery();
			while (rs.next()) {
				returnValue.add(rs.getString("nbr"));
			}
			return returnValue;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * @param recordNbr
	 *            what record
	 * @param search
	 *            what column in db to be returned
	 * @return the specific information for the selected search
	 */
	public String getInfo(String recordNbr, String search) {
		ResultSet rs = null;
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement("select * from records where nbr = '"
					+ recordNbr + "'");
			rs = stmt.executeQuery();
			while (rs.next()) {
				return rs.getString(search);
			}
		} catch (SQLException e) {

			e.printStackTrace();
		}
		return "no existing record";
	}

	public boolean removeRecord(int recordNbr) {
		ResultSet rs = null;
		PreparedStatement stmt = null;
		try {
			stmt = conn.prepareStatement("delete from records where nbr = "
					+ "'" + recordNbr + "'");
			stmt.executeUpdate();// kan vara update

			return true;

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

	public boolean writeTo(int recordNbr, String information, String name) {
		ResultSet rs = null;
		PreparedStatement stmt = null;
		String oldText = null;
		try {
			stmt = conn.prepareStatement("select * from records where nbr = '"
					+ recordNbr + "'");
			rs = stmt.executeQuery();
			rs.next();
			if (rs.getString("doctor").compareTo(name) == 0
					|| rs.getString("nurse").compareTo(name) == 0) {
				oldText = rs.getString("information");

				StringBuilder creator = new StringBuilder();
				creator.append(oldText);
				creator.append(", " + information);
				String newText = creator.toString();
				stmt = conn
						.prepareStatement("update records set information = '"
								+ newText + "' where nbr = '" + recordNbr + "'");
				stmt.execute();
				return true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}

	public boolean createRecord(String patient, String doctor, String nurse,
			String division) {
		ResultSet rs = null;
		PreparedStatement stmt = null;
		String oldText = null;
		try {
			stmt = conn
					.prepareStatement("insert into records (patient, doctor, nurse, division) values('"
							+ patient
							+ "', '"
							+ doctor
							+ "', '"
							+ nurse
							+ "', '"
							+ division + "')");
			stmt.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return false;
	}
	
	public boolean updateLog(String text){
		try{
		PreparedStatement stmt = conn.prepareStatement("insert into logfile (information) values('" + text + "')");
		stmt.executeUpdate();
		return true;
		}catch(SQLException e){
			e.printStackTrace();
		}
		return false;
	}

}
